import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  }, {
    path: '/home',
    name: 'home',
    component: () => import('@/views/movieHome.vue')
  }, {
    path: '/detail',
    name: 'movie-detail',
    component: () => import('@/views/movieDetail.vue'),
    props: true
  }, {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  }, {
    path: '/admin',
    name: 'admin',
    component: () => import('@/views/admin/home.vue'),
    children: [
      {
        path: 'list-movie',
        name: 'list-movie',
        component: () => import('@/views/admin/movies/movielist.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

/**
 * 全局前置守卫，实现客户端的后台管理页面守卫，登陆后访问
 */
router.beforeEach((to, from, next) => {
  console.log("路由beforeEach方法：", to, "当前登录用户：", localStorage);
  let token = localStorage.getItem("usertoken");
  if (to.fullPath.startsWith("/admin") && token == null) {
    console.log("未进行后台管理登录，跳转到登录");
    next({ path: '/login' })
  }
  next();
})

export default router
